package com.ruksis.motogeek;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

public class MotoServiceConnection implements ServiceConnection {

	private Messenger _mActivity;
	private Messenger _mService;
	
	public MotoServiceConnection(Handler activityHandler) {
		super();
		_mActivity = new Messenger(activityHandler);
	}
	
	public void startDataCollection() {
		Message msg = Message.obtain(null, MotoService.START_DATA_COLLECTION);
        msg.replyTo = _mActivity;
        try {
			_mService.send(msg);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	public void stopDataCollection() {
		Message msg = Message.obtain(null, MotoService.STOP_DATA_COLLECTION);
        msg.replyTo = _mActivity;
        try {
			_mService.send(msg);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	public void onServiceConnected(ComponentName name, IBinder service) {
		_mService = new Messenger(service);
		Message msg = Message.obtain(null, MotoService.REGISTER_CLIENT);
        msg.replyTo = _mActivity;
        try {
			_mService.send(msg);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
	}

	public void onServiceDisconnected(ComponentName name) {
		Message msg = Message.obtain(null, MotoService.UNREGISTER_CLIENT);
        msg.replyTo = _mActivity;
        try {
			_mService.send(msg);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

}
